<template>
  <div>
    <Modal
      :value="modalState"
      :title="title"
      :loading="loading"
      @on-ok="handleSubmit"
      @on-cancel="cancel"
      width="600">
      <Form
        ref="customerModal"
        :model="row"
        :rules="ruleInline"
        inline
        style="width: 100%"
        :label-width="80">
        <FormItem prop="name" label="客户姓名" style="width: 250px">
          <Input type="text" v-model="row.name" placeholder="请输入客户姓名" />
        </FormItem>
        <FormItem prop="tel" label="手机号" style="width: 250px">
          <Input type="text" v-model="row.tel" placeholder="请输入手机号" />
        </FormItem>
        <FormItem prop="price" label="放款金额" style="width: 250px">
          <Input type="text" v-model="row.price" placeholder="请输入放款金额" />
        </FormItem>
        <FormItem v-show="!row.id" prop="proid" label="项目id" style="width: 250px">
          <Select v-model="row.proid" clearable placeholder="请选择项目ID">
            <Option v-for="item in project" :value="item.id" :key="item.id">
              {{ item.name }}
            </Option>
          </Select>
        </FormItem>
        <!-- <FormItem prop="ordtime" label="放款时间" style="width: 250px">
           <DatePicker v-model="row.ordtime" type="datetime" placeholder="请输入放款时间"></DatePicker>
        </FormItem> -->
        <FormItem prop="repeat" label="是否复借" style="width: 250px">
          <Select v-model="row.repeat" clearable placeholder="请选择是否复借">
            <Option value="1">
              是
            </Option>
            <Option value="0">
              否
            </Option>
          </Select>
        </FormItem>
        <FormItem prop="pcode" label="渠道" style="width: 250px">
          <Input type="text" v-model="row.pcode" placeholder="请输入渠道" />
        </FormItem>
        <!-- <FormItem prop="uname" label="客服" style="width: 250px">
          <Input type="text" v-model="row.uname" placeholder="请输入客服姓名" />
        </FormItem>
        <FormItem prop="pname" label="客服主管" style="width: 250px">
          <Input type="text" v-model="row.pname" placeholder="请输入客服主管" />
        </FormItem> -->
        <FormItem prop="type" label="类型" style="width: 250px">
          <Select v-model="row.type" clearable placeholder="请选择类型">
            <Option value="P ">
              通过
            </Option>
            <Option value="F ">
              拒绝
            </Option>
          </Select>
        </FormItem>
        <FormItem prop="mstate" label="机审结果" style="width: 250px">
          <Select v-model="row.mstate" clearable placeholder="请选择机审结果">
            <Option value="1">
              通过
            </Option>
            <Option value="0">
              不通过
            </Option>
          </Select>
        </FormItem>
        <FormItem prop="des" label="备注" style="width: 100%">
          <Input v-model="row.des" type="textarea" placeholder="请输入备注" />
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
export default {
  name: 'CustomerModal',
  data () {
    return {
      title: '',
      loading: true,
      form: {
        name: '',
        price: '',
        // ordtime: '',
        repeat: '',
        pcode: '',
        uname: '',
        pname: '',
        type: '',
        mstate: '',
        des: ''
      },
      ruleInline: {
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        proid: [
          { type: 'number', required: true, message: '项目ID不能为空', trigger: 'change' }
        ]
      }
    }
  },
  updated () {
    if (this.row.id && this.row.id !== '') {
      this.title = '修改客户信息'
    } else {
      this.title = '添加客户记录'
    }
  },
  computed: {
    ...mapState({
      row: state => state.customer.row,
      modalState: state => state.modal.modalState,
      project: state => state.operate.project
    })
  },
  methods: {
    ...mapActions(['updateModalState']),
    changeLoading () {
      this.loading = false
      this.$nextTick(() => {
        this.loading = true
      })
    },
    handleSubmit () {
      this.$refs.customerModal.validate((valid) => {
        if (!valid) {
          return this.changeLoading()
        }
        this.$emit('customer-valid', {
          id: this.row.id,
          name: this.row.name,
          tel: this.row.tel,
          price: this.row.price !== '' ? this.row.price : '',
          proid: this.row.proid ? parseInt(this.row.proid) : '',
          // ordtime: this.row.ordtime ? this.$moment(this.row.ordtime).format('YYYY-MM-DD HH:mm:ss') : '',
          repeat: this.row.repeat ? parseInt(this.row.repeat) : '',
          pcode: this.row.pcode ? this.row.pcode : '',
          // uname: this.row.uname ? this.row.uname : '',
          // pname: this.row.pname ? this.row.pname : '',
          type: this.row.type ? this.row.type : '',
          mstate: this.row.mstate ? this.row.mstate : '',
          des: this.row.des ? this.row.des : ''
        })
        this.$refs['customerModal'].resetFields()
      })
    },
    cancel () {
      this.updateModalState(false)
      this.$refs['customerModal'].resetFields()
    }
  }
}
</script>
